package com.triumphxx.readingreminderbackend.entity;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author:triumphxx
 * @Date: 2025/11/20
 * @Time: 16:51
 * @微信公众号：思考集结处
 * @网站：http://blog.triumphxx.com.cn
 * @GitHub https://github.com/triumphxx
 * @Desc: 笔记相关信息
 **/

@Data
public class Note {
    private Long id;
    private String title;
    private String yuqueUrl;
    private String content;
    private LocalDateTime createdAt;
    private String folder;
    private LocalDateTime lastReminded;
    private String userId = "default";
    private LocalDateTime dbCreatedAt;
    private LocalDateTime dbUpdatedAt;

    // 关联的复习计划
    private List<ReviewSchedule> reviewSchedules;

    // 计算艾宾浩斯复习日期
    public void calculateReviewSchedules() {
        if (this.reviewSchedules == null) {
            this.reviewSchedules = new java.util.ArrayList<>();
        }

        // 清除现有的复习计划
        this.reviewSchedules.clear();

        // 艾宾浩斯复习间隔（天）
        int[] intervals = {1, 8, 22, 52};

        for (int i = 0; i < intervals.length; i++) {
            ReviewSchedule schedule = new ReviewSchedule();
            schedule.setStage(i + 1);
            schedule.setScheduledDate(this.createdAt.plusDays(intervals[i]));
            schedule.setCompleted(false);
            schedule.setNoteId(this.id);
            this.reviewSchedules.add(schedule);
        }
    }

    // 获取今天需要复习的计划
    public List<ReviewSchedule> getTodayReviews() {
        LocalDateTime today = LocalDateTime.now();
        LocalDateTime startOfDay = today.toLocalDate().atStartOfDay();
        LocalDateTime endOfDay = today.toLocalDate().plusDays(1).atStartOfDay();

        List<ReviewSchedule> todayReviews = new java.util.ArrayList<>();
        if (this.reviewSchedules != null) {
            for (ReviewSchedule schedule : this.reviewSchedules) {
                if (!schedule.getCompleted() &&
                        !schedule.getScheduledDate().isBefore(startOfDay) &&
                        schedule.getScheduledDate().isBefore(endOfDay)) {
                    todayReviews.add(schedule);
                }
            }
        }
        return todayReviews;
    }
}
